import Koa, { Context } from "koa";
import Router from "koa-router";
import { success, fail } from "../common/ResResult";
// addUser, findAllUser,findByProps,findByLike findByUsmAndAddr,countTotal
import userDao, { Userinfo } from "../modules/userinfo/dao/UserDao";
// import userDaoOrm from '../modules/decormodel/UserInfo'

const router = new Router();
// 路由前缀
router.prefix("/userModule");

// 添加用户
router.post("/addUser", async (ctx) => {
  // 获取参数
  const userinfo: Userinfo = ctx.request.body;
  // 添加到数据库
  ctx.body = success(await userDao.addUser(userinfo));
});
// 按地址计算所有用户
router.get("/countTotal", async (ctx: Koa.Context) => {
  ctx.body = success(await userDao.countUserinfo());
});
// 条件查询
router.get("/findByProps", async (ctx: Koa.Context) => {
  ctx.body = success(await userDao.findByProps());
});

router.get("/findByUsmAndAddr", async (ctx: Context) => {
  ctx.body = success(userDao.findByUsmAndAddr());
});
// 分页
router.get("/findUserWithPager/:pageNo/:pageSize", async (ctx: Context) => {
  const { pageNo, pageSize } = ctx.params;
  const offset = (pageNo - 1) * pageSize;
  // pageSize 是字符串
  ctx.body = success(
    await userDao.findUserWithPager(offset, parseInt(pageSize))
  );
});
router.get("/findByLike/:key", async (ctx: Context) => {
  const { key } = ctx.params;
  ctx.body = success(await userDao.findByLike(key));
});

// 
router.get("/findLikeOrm/:key", async (ctx: Context) => {
  const { key } = ctx.params;
  // ctx.body = success(await userDaoOrm.findByLike(key));
});
module.exports = router;
